package com.etoak.controller;

import com.etoak.common.core.vo.ResultVO;
import com.etoak.dto.CartDTO;
import com.etoak.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;

    /**
     * post /cart
     */
    @PostMapping
    public ResultVO<Object> saveOrUpdate(@Validated @RequestBody CartDTO cartDTO) {
        cartService.saveOrUpdate(cartDTO);
        return ResultVO.success();
    }

    /**
     * get /cart
     */
    @GetMapping("/list")
    public ResultVO<List<CartDTO>> list(){
        List<CartDTO> cartDTOList = cartService.list();
        return ResultVO.success(cartDTOList);
    }

    /**
     * delete /cart/dishes/{id}
     */
    @DeleteMapping("/dishes/{id}")
    public ResultVO<Object> deleteDishes(@PathVariable String id){
        cartService.deleteDishes(id);
        return ResultVO.success();
    }

    /**
     * delete /cart
     */
    @DeleteMapping
    public ResultVO<Object> clear(){
        cartService.clearCart();
        return ResultVO.success();
    }
}
